package webmarket.common;

import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Marker;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;

import webmarket.model.Shopcategory;

/**
 * 
 * Class for generally used methods
 * 
 */
public class CommonMethods {
	/**
	 * Calculates data for paging
	 */
	public static void addDefaultPagingDataToModel(ModelMap model, int page, long count, String currentPage, String prefix) {
		long pagecount = count / Constants.PAGE_SIZE;
		if (count % Constants.PAGE_SIZE != 0) {
			pagecount++;
		}
		model.put(prefix + "findcount", count);
		model.put(prefix + "pagecount", pagecount);
		model.put(prefix + "minshown", Math.max(1, page - Constants.PAGING_DIFFERENCE));
		model.put(prefix + "maxshown", Math.min(pagecount, page + Constants.PAGING_DIFFERENCE));
		model.put(prefix + "pagingDiff", Constants.PAGING_DIFFERENCE);
		model.put(prefix + "currentPage", currentPage);
	}

	/**
	 * Calculates data for paging
	 */
	public static void addDefaultPagingDataToModel(ModelMap model, int page, long count, String currentPage) {
		addDefaultPagingDataToModel(model, page, count, currentPage, "");
	}

	/**
	 * Ordering an array
	 * 
	 * @param array
	 *            input array to sort
	 * @return the sorted set
	 */
	public static <T extends Comparable<T>> SortedSet<T> getOrdered(T[] array) {
		SortedSet<T> set = new TreeSet<T>();
		for (T element : array) {
			set.add(element);
		}
		return set;
	}

	/**
	 * 
	 * Get the username of the currently logged in, active user
	 * 
	 * @return String name of the current user if logged in
	 */
	public static String getCurrentUserName() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication.isAuthenticated()) {
			return authentication.getName();
		} else {
			return null;
		}
	}

	/**
	 * Find if the currently active user is logged in or not
	 * 
	 * @return boolean true if the user is logged in
	 */
	public static boolean isCurrentUserLoggedIn() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		return authentication.isAuthenticated();
	}

	public static String printMarker(Marker marker) {
		if (marker == null) {
			return "";
		} else {
			return printMarker(marker.getParent()) + "/" + MarkerController.getName(marker.getName());
		}
	}

	public static String printCategory(Shopcategory category) {
		if (category.getParent() == null) {
			return "";
		}
		return printCategory(category.getParent()) + "/" + category.getName();
	}

	public static String getIcon(Shopcategory category) {
		if (!StringUtils.isBlank(category.getIcon())) {
			return category.getIcon();
		} else if (category.getParent() != null) {
			return getIcon(category.getParent());
		} else {
			return null;
		}
	}
}
